﻿Option Explicit On

'*******************************************************************
' Program: xmlVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Working with visio drawing in an xml format
'
'
'
'*******************************************************************

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports System.Text.StringBuilder

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System.IO
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.Linq
Imports System.Xml.XPath

Imports <xmlns="http://schemas.microsoft.com/visio/2003/core">
Imports <xmlns:vx="http://schemas.microsoft.com/visio/2006/extension">
Imports <xmlns:v14="http://schemas.microsoft.com/office/visio/2010/extension">



Public Class frmRuleSets

#Region " Properties and Declarations "


    '
    ' where are we keeping the drawings
    '
    Private _dataPath As String
    Public Property dataPath As String
        Get
            dataPath = _dataPath
        End Get
        Set(value As String)
            _dataPath = value
        End Set
    End Property

    '
    ' what is the latest document
    '
    Public _xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me._xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me._xDoc = value
        End Set
    End Property




#End Region


    '
    ' load a list of ruleset names and ids into a list control
    '
    Public Sub getRuleSets _
        (ByRef lvControl As ListView)

        Try

            If lvControl Is Nothing Then lvControl = Me.lvRuleSets

            Dim qryRuleSet = xDoc...<v14:RuleSet>
            Dim intRuleSet As Integer = qryRuleSet.Count

            lvControl.Items.Clear()
            lvControl.Columns.Clear()
            lvControl.View = View.Details
            lvControl.AllowColumnReorder = True
            lvControl.FullRowSelect = True
            lvControl.Columns.Add("Id", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("NameU", 100, HorizontalAlignment.Left)
            lvControl.Columns.Add("Descr", 100, HorizontalAlignment.Left)

            Dim lvItem As ListViewItem

            For Each ruleset In qryRuleSet

                Dim lngRuleSetId As Long = -1
                Dim strRuleSetNameU As String = "NoRuleSetNameU"
                Dim strRuleSetName As String = "NoRuleSetName"
                Dim strRuleSetDescription As String = "NoRuleSetDescription"


                Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In ruleset.Attributes() _
                    Select att
                For Each att As XAttribute In listOfAttributes
                    ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                    Select Case LCase(att.Name.LocalName)
                        Case "id"
                            lngRuleSetId = CLng(att.Value)
                        Case "nameu"
                            strRuleSetNameU = CStr(att.Value)
                        Case "description"
                            strRuleSetDescription = CStr(att.Value)
                    End Select
                Next att

                lvItem = New ListViewItem(lngRuleSetId)
                lvItem.SubItems.Add(strRuleSetNameU)
                lvItem.SubItems.Add(strRuleSetDescription)

                ' add it to the listview on the data page
                lvControl.Items.Add(lvItem)

                ' do the init stuff for the first record

                If lvControl.Items.Count = 1 And _
                    lvControl.Name = "lvRuleSets" Then

                    getRules(ruleset, Me.lvRules)
                    Me.txtRuleSetId.Text = CStr(lngRuleSetId)
                    Me.txtRuleSetNameU.Text = strRuleSetNameU
                    Me.txtRuleSetDescription.Text = strRuleSetDescription

                End If
            Next ruleset

        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub lvRuleSets_SelectedIndexChanged(sender As System.Object, e As System.EventArgs)

        Dim rulesetItems As ListView.SelectedListViewItemCollection = lvRuleSets.SelectedItems
        Dim rulesetItem As ListViewItem
        Dim lngId As Long

        If 0 < rulesetItems.Count Then

            rulesetItem = rulesetItems(0)
            Me.txtRuleSetId.Text = rulesetItem.SubItems(0).Text
            Me.txtRuleSetNameU.Text = rulesetItem.SubItems(1).Text
            Me.txtRuleSetDescription.Text = rulesetItem.SubItems(2).Text

            ' query the rules
            Dim qryRules = From rulesetSel In Me.xDoc...<v14:RuleSet> _
                    Where rulesetSel.@ID = rulesetItem.SubItems(0).Text

            If qryRules.Count = 1 Then
                getRules(qryRules.First, Me.lvRules)
            Else
                MsgBox("could not find ruleset")
            End If ' test that only one ruleset was found

        End If ' test for something selected

    End Sub

    '
    ' load a list of rule names and ids into a list control
    '
    Public Sub getRules _
        (ByVal ruleSource As XElement, _
         ByRef lvControl As ListView)

        Try

            If lvControl Is Nothing Then lvControl = Me.lvRuleSets

            Dim qryRules = ruleSource...<v14:Rule>
            Dim intRules As Integer = qryRules.Count

            lvControl.Items.Clear()
            lvControl.Columns.Clear()
            lvControl.View = View.Details
            lvControl.AllowColumnReorder = True
            lvControl.FullRowSelect = True
            lvControl.Columns.Add("Id", 50, HorizontalAlignment.Right)
            lvControl.Columns.Add("NameU", 100, HorizontalAlignment.Left)
            lvControl.Columns.Add("Descr", 100, HorizontalAlignment.Left)


            For Each rules In qryRules

                Dim lvItem As ListViewItem

                Dim lngRuleId As Long = -1
                Dim strRuleNameU As String = "NoRuleNameU"
                Dim strRuleName As String = "NoRuletName"
                Dim strRuleDescription As String = "NoRuleDescription"

                ' first go through the attributes for the rule

                Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In rules.Attributes() _
                    Select att
                For Each att As XAttribute In listOfAttributes
                    ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                    Select Case LCase(att.Name.LocalName)
                        Case "id"
                            lngRuleId = CLng(att.Value)
                        Case "nameu"
                            strRuleNameU = CStr(att.Value)
                        Case "description"
                            strRuleDescription = CStr(att.Value)
                    End Select
                Next att

                ' build the listview control
                lvItem = New ListViewItem(lngRuleId)
                lvItem.SubItems.Add(strRuleNameU)
                lvItem.SubItems.Add(strRuleDescription)

                ' add it to the listview on the data page
                lvControl.Items.Add(lvItem)


                ' do the init stuff for the first record
                If lvControl.Items.Count = 1 And _
                    lvControl.Name = "lvRules" Then

                    loadRuleFormInfo(rules)

                End If
            Next rules

        Catch errXml As XmlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(errXml, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Catch err As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(err, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub lvRules_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles lvRules.SelectedIndexChanged


        Dim ruleItems As ListView.SelectedListViewItemCollection = lvRules.SelectedItems
        Dim ruleItem As ListViewItem
        Dim lngId As Long

        If 0 < ruleItems.Count Then

            ruleItem = ruleItems(0)

            ' query the rules
            Dim qryRules = From ruleSel In Me.xDoc...<v14:Rule> _
                    Where ruleSel.@ID = ruleItem.SubItems(0).Text

            If qryRules.Count = 1 Then
                loadRuleFormInfo(qryRules.First)
            Else
                MsgBox("could not find rule")
            End If ' test that only one ruleset was found


        End If ' test for something selected

    End Sub


    '
    '
    '
    Private Sub loadRuleFormInfo _
        (ByVal ruleSource As XElement)


        Dim lngRuleId As Long = -1
        Dim strRuleNameU As String = "NoRuleNameU"
        Dim strRuleName As String = "NoRuletName"
        Dim strRuleDescription As String = "NoRuleDescription"
        Dim strRuleCategory As String = "NoRuleCategory"
        Dim lngRuleTarget As Long = -1
        Dim strRuleFilter As String = "NoRuleFilter"
        Dim strRuleTest As String = "NoRuleTest"

        Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                From att In ruleSource.Attributes() _
                Select att

        For Each att As XAttribute In listOfAttributes
            ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
            Select Case LCase(att.Name.LocalName)
                Case "id"
                    lngRuleId = CLng(att.Value)
                Case "nameu"
                    strRuleNameU = CStr(att.Value)
                Case "description"
                    strRuleDescription = CStr(att.Value)
                Case "category"
                    strRuleCategory = CStr(att.Value)
                Case "target"
                    lngRuleTarget = CLng(att.Value)
            End Select
        Next att


        Dim listOfElements As IEnumerable(Of XElement) = _
                From elemData In ruleSource.Elements() _
                Select elemData

        If listOfElements.Count > 0 Then
            For Each elem As XElement In listOfElements
                ' Console.WriteLine("{0}={1}", elem.Name.LocalName, elem.Value)
                Select Case LCase(elem.Name.LocalName)
                    Case "rulefilter"
                        strRuleFilter = CStr(elem.Value)
                    Case "ruletest"
                        strRuleTest = CStr(elem.Value)
                End Select
            Next elem

        End If

        Me.txtRuleId.Text = CStr(lngRuleId)
        Me.txtRuleNameU.Text = strRuleNameU
        Me.txtRuleDescription.Text = strRuleDescription
        Me.txtRuleCategory.Text = strRuleCategory
        Me.txtRuleTarget.Text = CStr(lngRuleTarget)
        Me.txtRuleFilter.Text = CStr(strRuleFilter)
        Me.txtRuleTest.Text = CStr(strRuleTest)


    End Sub


    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub

End Class